#include "CPatient.h"
#include "progresswindow.h"
#include <cstdio>
#include <cstdlib>
#include <QFile>
#include <QString>
#include <QDebug>



int CPatient::load (string dpath)
{
	char x[3];
        QString tempName (dpath.c_str());
        string a,b;
        string path= dpath+ "\\1_1_Gray.txt";
        int count=0;

        qDebug() << "dpath = " << tempName;
        tempName.replace(QString("\\"), QString("_"));
        qDebug() << tempName;
        tempName.remove(0,3);
        qDebug() << tempName;
        patientName = tempName.toStdString();
        qDebug() << "patientName =" << patientName.c_str();

        for (int i=1; i<100; i++)
                for (int j=1; j<100; j++)
                {
                        a = QString::number(i, 10).toStdString();
                        //a=x;
                        b = QString::number(j, 10).toStdString();
                        //b=x;
                        string pathr=dpath + "\\" + a + "_" + b + "_Red.txt";
                        string pathg=dpath + "\\" + a + "_" + b + "_Green.txt";
                        string pathb=dpath + "\\" + a + "_" + b + "_Blue.txt";
                        if (fopen(pathr.c_str(), "r")!=NULL)
                        {
                            count++;
                        }
                        else break;
                }

        ProgressWindow *progress = new ProgressWindow ("Loading Patient files...", count);
        progress->showNormal();
        int cc=0;
      if(false) //(fopen(path.c_str(), "r")!=NULL)
        {
//            for (int i=0; i<100; i++)
//                    for (int j=0; j<100; j++)
//                    {
//                            itoa (i, x, 10);
//                            a=x;
//                            itoa (j, x, 10);
//                            b=x;
//                            path=dpath + "\\" + a + "_" + b + "_Gray.txt";
//                            if (fopen(path.c_str(), "r")!=NULL)
//                            {
//                                    CCell temp;
//                                    temp.load(path);
//                                    files.push_back(path);
//                                    cells.push_back(temp);
//                                    //temp.clear();
//                                    cc++;
//                                    progress->setPosition(cc);
//                            }
//                            else break;
//                    }
        }
        else
	{
                for (int i=1; i<100; i++)
                        for (int j=1; j<100; j++)
			{
                                /*itoa (i, x, 10);
				a=x;
				itoa (j, x, 10);
                                b=x;*/
                                a = QString::number(i, 10).toStdString();
                                b = QString::number(j, 10).toStdString();
				string pathr=dpath + "\\" + a + "_" + b + "_Red.txt";
				string pathg=dpath + "\\" + a + "_" + b + "_Green.txt";
				string pathb=dpath + "\\" + a + "_" + b + "_Blue.txt";
                                if (fopen(pathr.c_str(), "r")!=NULL)
				{

                                        files.push_back(pathr);
                                        cells.resize(cells.size()+1);
                                        cells[cells.size()-1].load(pathr,pathg,pathb);
                                        cells[cells.size()-1].genMask();
                                        cells[cells.size()-1].getDelta();
                                        cc++;
                                        progress->setPosition(cc);
				}
				else break;
			}
	}
        delete progress;
        //progress->~QObject();
	return 0;
}


int CPatient::patientFile()
{
    QFile outfile;
    QString resFile("E:\\");
    resFile.append(this->patientName.c_str());
    resFile.append(".txt");
    FILE *file = fopen (resFile.toAscii(), "w");
    outfile.open(file, QIODevice::WriteOnly);
    ProgressWindow *progress = new ProgressWindow ("Creating Patient file...", cells.size());
    progress->show();
    for (int i=0; i<cells.size(); i++)
    {
        vector<double> signalsCalc;
        signalsCalc = cells[i].getSignals();
        QString temp("");
        for (int j=0; j<signalsCalc.size(); j++)
        {
            temp=temp + QString::number(signalsCalc[j], 'f', 5) + ((j==signalsCalc.size()-1)?"\n":" ");
        }
        outfile.write(temp.toUtf8());
        qDebug("Finished calculations");
        progress->setPosition(i);
    }
    outfile.close();
    delete progress;
    return 0;
}




















